package monster.com.cvh.activity;

import android.graphics.Color;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import monster.com.cvh.R;
import monster.com.cvh.activity.base.PermissionActivity;
import monster.com.cvh.adapter.PraiseAdapter;
import monster.com.cvh.bean.PraiseBean;
import monster.com.cvh.util.MyConstant;
import monster.com.cvh.util.RecyclerViewDivider;
import monster.com.cvh.util.SPUtils;
import monster.com.cvh.util.StatusBarUtil;
import okhttp3.Call;
import okhttp3.Response;

/**
 * @author: monster
 * @data: 2018/4/2
 * @function:点赞界面
 */
public class PraiseActivity extends PermissionActivity {
    @BindView(R.id.iv_news_details_title_back)
    ImageView ivNewsDetailsTitleBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.details_title)
    RelativeLayout detailsTitle;
    @BindView(R.id.rl_activity_praise)
    RecyclerView rlActivityPraise;
    @BindView(R.id.rf_activity_praise)
    SmartRefreshLayout rfActivityPraise;
    private List<PraiseBean.DataBean> mDatas = new ArrayList<>();
    private int page = 0 ;
    private PraiseAdapter adapter;

    @Override
    protected int getContentViewResId() {
        return R.layout.activity_praise;
    }

    @Override
    protected void initEvent() {

    }

    /**
     * 获取赞
     *
     * @param refreshlayout 刷新
     * @param b
     */
    private void refreshAlllayout(final RefreshLayout refreshlayout, final boolean b) {
        refreshlayout.getLayout().postDelayed(new Runnable() {
            @Override
            public void run() {
                if (b) {
                    if (mDatas.size() > 0) {
                        mDatas.clear();
                        page = 0;
                    }
                    getPraise( page);
                    rlActivityPraise.setAdapter(adapter);
                    rfActivityPraise.finishRefresh();
                    page = 0;

                } else {
                    getPraise( page);
                    rfActivityPraise.finishLoadmore();
                    page++;
                }
            }
        }, 2000);
    }

    /**
     * 获取赞列表
     * @param page 页数
     */
    private void getPraise(int page) {
        OkGo.<String>get(MyConstant.GET_PRIASE_LIST).params("token", SPUtils.getString(PraiseActivity.this,"token",""))
                .params("page_index",page).execute(new StringCallback() {
            @Override
            public void onSuccess(String s, Call call, Response response) {
                Gson gson = new Gson();
                PraiseBean praiseBean = gson.fromJson(s, PraiseBean.class);
                switch (praiseBean.getCode()){
                    case 1:
                        List<PraiseBean.DataBean> data = praiseBean.getData();
                        if(data != null && data.size()>0){
                            adapter.addData(data);
                        }
                        break;
                }
            }
        });
    }

    @Override
    protected void initData() {
        //修改状态栏颜色
        StatusBarUtil.setStatusBarLightMode(PraiseActivity.this, Color.WHITE);
        tvTitle.setText("赞");
        rfActivityPraise.autoRefresh();
        rlActivityPraise.setLayoutManager(new LinearLayoutManager(this));
        adapter = new PraiseAdapter(R.layout.item_praise, mDatas);
        rlActivityPraise.addItemDecoration(new RecyclerViewDivider(this, LinearLayoutManager.HORIZONTAL, 2, Color.parseColor("#888888")));
        rfActivityPraise.setOnRefreshLoadmoreListener(new OnRefreshLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                refreshAlllayout(refreshlayout, false);
            }

            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                refreshAlllayout(refreshlayout, true);
            }
        });

    }


    @OnClick(R.id.iv_news_details_title_back)
    public void onViewClicked() {
        finish();
    }

}
